package com.tpp.threat_perception_platform.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tpp.threat_perception_platform.dao.HostSystemVulMapper;
import com.tpp.threat_perception_platform.param.MyParam;
import com.tpp.threat_perception_platform.pojo.HostSystemVul;

import com.tpp.threat_perception_platform.pojo.HostVul;
import com.tpp.threat_perception_platform.response.ResponseResult;
import com.tpp.threat_perception_platform.service.HostSystemVulService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
public class HostSystemVulServiceImpl implements HostSystemVulService {
    @Autowired
    private HostSystemVulMapper hostSystemVulMapper;

    @Override
    public int saveHostSystemVul(HostSystemVul hostSystemVul) {
        // 设置公共属性
        hostSystemVul.setDetectTime(new Timestamp(System.currentTimeMillis()));
        // 插入新记录
        return hostSystemVulMapper.insertSelective(hostSystemVul);
    }

    @Override
    public void deleteByMacAddress(String macAddress) {
        hostSystemVulMapper.deleteByMacAddress(macAddress);
    }

    @Override
    public ResponseResult hostSystemVulList(MyParam param) {
        PageHelper.startPage(param.getPage(), param.getLimit());

        List<HostSystemVul> dataList;

        if (param.getMacAddress() != null && !param.getMacAddress().isEmpty()) {
            dataList = hostSystemVulMapper.findByMacAddress(param);
        } else if (param.getHostName() != null && !param.getHostName().isEmpty()) {
            dataList = hostSystemVulMapper.findByHostName(param);
        } else if (param.getSystemVulName() != null && !param.getSystemVulName().isEmpty()) {
            dataList = hostSystemVulMapper.findBySystemVulName(param);
        } else if (param.getStatus() != null && !param.getStatus().isEmpty()) {
            dataList = hostSystemVulMapper.findByStatus(param);
        } else {
            dataList = hostSystemVulMapper.findAll(param);
        }

        PageInfo<HostSystemVul> pageInfo = new PageInfo<>(dataList);
        return new ResponseResult<>(pageInfo.getTotal(), pageInfo.getList());
    }

}
